<template>
  <div class="u-p-20">
    <el-card shadow="never">
      <div slot="header">
        <b class="u-f-16"> 添加代理商收益 </b>
        <div class="u-right u-5mt">
          <el-button
            type="success"
            size="small"
            icon="el-icon-document-checked"
            @click="submitForm('objForm')"
            v-if="hasPerm('agencyEarningsAddPost')"
            >提交保存</el-button
          >
          <el-button size="small" icon="el-icon-back" @click="$router.go(-1)"
            >返回</el-button
          >
        </div>
      </div>
      <el-form
        :model="objForm"
        :rules="rules"
        ref="objForm"
        label-width="100px"
        class="u-form"
      >
        <el-form-item label="结算时间区间">
          <el-date-picker
            v-model="objForm.time_section"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="yyyy-MM-dd"
            @change="getPrice"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="收益金额" prop="price">
          <el-input v-model="objForm.price" placeholder="收益金额"></el-input>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import commonEdit from "@/mixins/common-edit";

export default {
  mixins: [commonEdit],
  data() {
    return {
      apiName: "agency",
      backRoute: "agencyEarnings", //成功跳转地址
      fileList: [], //临时代理商图片
      fileList1: [], //临时头像
      objForm: {
        id: "",
        agency_id: 0,
        price: 0, //收益金额
        time_section: [], //时间区间
      },
    };
  },
  methods: {
    getInfo() {},
    submitForm(formName) {
      if (!this.submitFormEnd) {
        return;
      }
      this.submitFormEnd = false;
      const self = this;
      self.$refs[formName].validate((valid) => {
        if (valid) {
          self.beforeSubmit();
          self.$api[self.apiName].earningsAdd(self.objForm).then((response) => {
            this.submitFormEnd = true;
            if (response.code == 1) {
              self.$message({
                message: "操作成功",
                type: "success",
              });
              self.$router
                .push({
                  name: self.backRoute,
                  params: { page: self.currentPage, id: self.backPid },
                })
                .catch(() => {});
              self.submitEnd(1);
            } else {
              self.submitEnd(0);
            }
          });
        } else {
          this.submitFormEnd = true;
          return false;
        }
      });
    },
    getPrice() {
      //获取经纬度
      const self = this;
      self.$api[self.apiName]
        .earningsAdd({
          time_section: self.objForm.time_section,
          agency_id: self.objForm.agency_id,
          is_ajax: "is_ajax",
        })
        .then((response) => {
          if (response.code == 1) {
            self.objForm.price = response.data;
          } else {
            self.$message({
              message: response.msg,
              type: "error",
            });
          }
        });
    },
  },

  created() {
    if (this.$route.params.id) {
      this.objForm.agency_id = this.$route.params.id;
    }
    this.getPrice();
  },
};
</script>
<style>
.distpicker-address-wrapper select {
  font-size: 100%;
}
</style>
